package pt.inescid.components.policyengine.heimdall.pap;

import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.Policy;
import pt.inescid.components.policyengine.heimdall.base.User;
import pt.inescid.components.policyengine.heimdall.database.PolicyDAO;
import pt.inescid.components.policyengine.heimdall.database.UserDAO;
import pt.inescid.components.policyengine.heimdall.pap.base.CesDefinition;
import pt.inescid.components.policyengine.heimdall.pap.base.DomainDefinition;
import pt.inescid.components.policyengine.heimdall.pap.base.ModelDefinition;
import pt.inescid.components.policyengine.heimdall.pap.base.PolicyDefinition;

public class UserManagerAS {

	private static Vector allUsers = null;
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      getRecord       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static User getRecord(long recordId) {
		if (allUsers == null) {
			updateUsers();
		}
		
		User currentUser = null;
		for (int i = 0; i < allUsers.size(); i++) {
			currentUser = (User) allUsers.elementAt(i);
			if (currentUser.getId() == recordId) {
				return (currentUser);
			}
		}

		return (null);
	} // getRecord


	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      getLoggedInUsers       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static Vector getLoggedInUsers() {
//		if (allUsers == null) {
			updateUsers();
//		}
		
		return (allUsers);
	} // getLoggedInUsers

	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      getAllUsers       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static Vector getAllUsers() {
		if (allUsers == null) {
			updateUsers();
		}
		
		return (allUsers);
	} // getAllUsers

	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      updateUsers       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static void updateUsers() {
		allUsers = UserDAO.getAllRecords();
		Vector allPolicies = PolicyDAO.getAllRecords();
		
		User currentUser = null;
		Policy currentPolicy = null;
		for (int i = 0; i < allUsers.size(); i++) {
			currentUser = (User) allUsers.elementAt(i);
			
			for (int j = 0; j < allPolicies.size(); j++) {
				currentPolicy = (Policy) allPolicies.elementAt(j);
				
				if (currentPolicy.getUser() == currentUser.getId()) {
					PolicyParser.updateModel(currentUser, 
							currentPolicy.getTag(),
							currentPolicy.getText());
					
					// if its a ces remote model,
					// strip the policy definition
					// and set the monitored ces
					if (currentPolicy.getType() == Policy.REMOTE_POLICY) {
						stripModelDefinition(currentUser.getModelByTag(
								currentPolicy.getTag()));

						currentUser.getModelByTag(currentPolicy.getTag()).
								setMonitoredCes(currentPolicy.getCesName());
					}
				}
				allPolicies.removeElementAt(j);
				j--;
			}
		}
	} // updateUsers
	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      stripModelDefinition       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static void stripModelDefinition(ModelDefinition currentModelDef) {
		
		// DOMAINS: A remote ces model is only issued to fetch local events 
		Vector localDomain = new Vector();
		localDomain.add(new DomainDefinition("localhost", "", true));
		
		// CES: keep only the currentCes and any associated parents.
		CesDefinition currentCes = currentModelDef.getCes(
				currentModelDef.getMonitoredCes());
		Vector allRelevantCes = new Vector();
		allRelevantCes.add(currentCes);
		
		while (currentCes.getBaseCes() != null) {
			currentCes = currentCes.getBaseCes();
			allRelevantCes.add(currentCes);
		}

		Vector allCes = currentModelDef.getCes();
		for (int i = 0; i < allCes.size(); i++) {
			currentCes = (CesDefinition) allCes.elementAt(i);
			if (!allRelevantCes.contains(currentCes)) {
				allCes.removeElementAt(i);
				i--;
			}
		}
		
		// RULES: remove all rules which contain irrelevant Ces.
		PolicyDefinition currentPolicy = null;
		for (int i = 0; i < currentModelDef.getPolicies().size(); i++) {
			currentPolicy = (PolicyDefinition) currentModelDef.getPolicies().
					elementAt(i);
			currentPolicy.keepOnlyThisCes(
					currentModelDef.getMonitoredCes());
		}
	} // stripModelDefinition
}; // UserManagerAS